<?php
/**
 * User: wyman
 * Date: 2020-06-28
 * Email: <yangww@wecut.com>
 */

namespace App\Service;

use App\Model\AdmModel;
use App\Support\Table\Column;
use App\Support\Table\ColumnCollection;
use App\Support\Type\DataType;

class AdmService extends Service
{
    /**
     * @var AdmModel
     */
    protected $model;

    const URL_FUNCTION_MAP = [
        'listUrl'   => 'getList',
//        'inputUrl'  => 'getInputs',   //默认取消inputUrl 直接读取tableHeaders 但是可以自定义添加 添加之后读取接口的信息
        'infoUrl'   => 'getInfo',
        'editUrl'   => 'modify',
        'createUrl' => 'modify',
        'deleteUrl' => 'delete',
    ];

    /**
     * 获取表信息
     * tableHeaders 表头列表
     * commonOperations 公共操作
     * listUrl   列表地址
     * inputUrl  输入框信息地址
     * infoUrl   详情信息地址
     * editUrl   编辑提交地址
     * createUrl 新增提交地址
     * deleteUrl 删除地址
     * @return array
     * @author LiWenYe
     */
    public function getTableInfo()
    {
        return $this->getTableInfoWithConditions();
    }

    // 表信息 带上选项/表名/参数
    public function  getTableInfoWithConditions($options = [], $tableName = '', $params = [])
    {
        $options || $options = $this->model->options;
        $columnCollection = (new ColumnCollection($this->model->columns))
            ->withOperations($this->model->operations)
            ->withCommonOperations($this->model->commonOperations)
            ->withOptions($options);

        $tableName || $tableName = DataType::camelize($this->model->getTable());

        $commonUrls = [];
        foreach (static::URL_FUNCTION_MAP as $key => $item) {
            if (empty($params)) {
                $commonUrls[$key] = '/adm/' . $tableName . '/' . $item;
            } else {
                $commonUrls[$key] = '/adm/' . $tableName . '/' . $item . '?' . http_build_query($params);
            }
        }

        return [
            'code' => 1,
            'data' => [
                'tableHeaders'     => $columnCollection->getTableHeaders(),
                'commonOperations' => $columnCollection->getCommonOperations(),
                'commonUrls'       => $commonUrls,
            ],
        ];
    }

    // 详情信息
    public function getInfo($id)
    {
        return [
            'code' => 1,
            'data' => $this->model->getInfo($id)
        ];
    }

    // 编辑|新增
    public function modify($id, $data)
    {
        if (isset($data['_FILES'])) {
            unset($data['_FILES']);
        }

        if (empty($id)) {
            $res = $this->model->newQuery()->forceCreate($data);
        } else {
            $res = $this->model->newQuery()->where('id', $id)->update($data);
        }

        if ($res !== false) {
            return [
                'code' => 1,
                'msg' => '操作成功',
            ];
        } else {
            return [
                'code' => 0,
                'msg' => '操作失败',
            ];
        }
    }

    // 分页列表信息
    public function getPageList($page, $limit, $params)
    {
        // 把列表中不显示的字段去除
        (new ColumnCollection($this->model->columns))->filter(function (Column $column) {
            if (($column->showStatus & 1) == 0) {
               unset($this->model->columns[$column->field]);
           }
        });

        $data = $this->model->getPageInfo($page, $limit, $params);

        return [
            'code' => 1,
            'data' => $data,
        ];
    }

    // 输入框信息
    public function getInputs()
    {
        return $this->getInputsWithConditions();
    }

    public function getInputsWithConditions($options = [])
    {
        $inputs = (new ColumnCollection($this->model->columns))
            ->withOperations($this->model->operations)
            ->withOptions($options)
            ->getFormInputs();

        $data['inputs'] = $inputs;

        return [
            'code' => 1,
            'data' => $data
        ];
    }

    // 删除
    public function delete($id)
    {
        $res = $this->model
            ->newQuery()
            ->where('id', $id)
            ->delete();

        if ($res !== false) {
            return [
                'code' => 1,
                'msg'  => '删除成功'
            ];
        } else {
            return [
                'code' => 0,
                'msg'  => '删除失败'
            ];
        }
    }

    // 获取model
    public function getModel()
    {
        return $this->model;
    }
}
